package com.ac.api.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.annotation.GroupUseridDataAuth;
import com.base.annotation.SysLog;
import com.base.helper.BaseController;
import com.base.helper.Result;
import com.ac.api.base.entity.SysMessage;
import com.ac.api.base.service.SysMessageService;
import com.ac.api.base.vo.SysMessageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mzl
 * @since 2020-11-14
 */
@Api(tags = "消息")
@RestController
@RequestMapping("/sysMessage")
public class SysMessageController extends BaseController {

    private static final Logger            LOG = LoggerFactory.getLogger(SysMessageController.class);
    @Autowired
    private              SysMessageService entityService;

    @SysLog(action = "sysMessage:findByPage", value = "分页查询消息")
    @ApiOperation(value = "分页查询消息", notes = "分页查询消息")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面：从1开始", paramType = "query")})
    public Result<IPage<SysMessageVo>> findByPage(final SysMessageVo example, final Page page) {
        IPage<SysMessageVo> records = entityService.findByPage(page, example);
        return new Result(true, records);
    }

    @SysLog(action = "sysMessage:delete", value = "删除消息")
    @ApiOperation(value = "删除消息")
    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "消息ID", required = true, paramType = "path")
    public Result delete(@PathVariable final Integer id) {
        SysMessage sysMessage = entityService.getById(id);
        if (null == sysMessage) {
            throw new RuntimeException("没有找到Id对应对象");
        }
        sysMessage.setState("0");
        entityService.updateById(sysMessage);
        return new Result(true, "成功删除消息", null);
    }

    @SysLog(action = "sysMessage:one", value = "查询单个消息")
    @ApiOperation(value = "查询单个消息")
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "消息ID", required = true, paramType = "path")
    public Result one(@PathVariable final Integer id) {
        SysMessageVo sysMessage = entityService.findById(id);
        return new Result(true, sysMessage);
    }

    @SysLog(action = "sysMessage:add", value = "添加消息")
    @ApiOperation(value = "添加消息")
    @RequestMapping(method = {RequestMethod.POST})
    public Result add(@Valid @RequestBody final SysMessage entity) {
        entityService.save(entity);
        return new Result(true, "成功保存消息", null);
    }

    @SysLog(action = "sysMessage:update", value = "修改消息")
    @ApiOperation(value = "修改消息")
    @RequestMapping(method = {RequestMethod.PUT})
    public Result update(@Valid @RequestBody final SysMessage entity) {
        if (null == entity.getId()) {
            throw new RuntimeException("id不能为空");
        }
        entityService.updateById(entity);
        return new Result(true, "成功修改消息", null);
    }

    @SysLog(action = "sysMessage:all", value = "全部消息")
    @ApiOperation(value = "全部消息")
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Result<List<SysMessageVo>> all(SysMessageVo example) {
        List<SysMessageVo> entitys = entityService.findByExample(example);
        if (null != entitys) {
            return new Result<>(true, entitys);
        }
        return new Result<>(true, Collections.emptyList());
    }

    @SysLog(action = "sysMessage:readMessage", value = "强制阅读消息")
    @ApiOperation("强制阅读消息")
    @RequestMapping(value = "readMessage", method = RequestMethod.POST)
    public Result readMessage(@Valid @RequestBody Integer[] ids) {
        entityService.batchRead(Arrays.asList(ids));
        return new Result(true, "强制阅读消息成功", null);
    }

    @GroupUseridDataAuth
    @SysLog(action = "sysMessage:noReadCountByUserId", value = "订单未读消息数")
    @ApiOperation(value = "订单未读消息数")
    @RequestMapping(value = "noReadCountByUserId/{userid}", method = RequestMethod.GET)
    public Result<Integer> noReadCountByUserId(@PathVariable("userid") final Integer userid) {
        Integer count = entityService.noReadCount(userid);
        return new Result(true, count);
    }


    @SysLog(action = "sysMessage:add", value = "批量推送消息")
    @ApiOperation(value = "批量推送消息")
    @RequestMapping(value = "pushMessage",method = {RequestMethod.POST})
    public Result pushMessage(@Valid @RequestBody final List<SysMessage> entitys) {
        entityService.saveBatch(entitys);
        return new Result(true, "成功推送消息", null);
    }


}

